package org.nerve.commons.util;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * org.nerve.commons.util
 * Created by zengxm on 2016/4/20 0020.
 */
public class DateUtil {
	public final static String YYYYMMDD="yyyy-MM-dd";
	public final static String YYYYMMDDHHMMSS="yyyy-MM-dd HH:mm:ss";

	private static Map<String, ThreadLocal<SimpleDateFormat>> sdfMap=new ConcurrentHashMap<>();

	private static SimpleDateFormat getFormatter(final String p){
		if(!sdfMap.containsKey(p)){
			sdfMap.put(p, new ThreadLocal<SimpleDateFormat>(){
				@Override
				protected SimpleDateFormat initialValue() {
					return new SimpleDateFormat(p);
				}
			});
		}

		return sdfMap.get(p).get();
	}

	public static String get(){
		return get(new Date());
	}

	public static String get(Date date){
		return get(date, YYYYMMDD);
	}

	public static String get(Date date,String formatter){
		SimpleDateFormat dateFormat=getFormatter(formatter);

		return dateFormat.format(date);
	}
}
